<?php
class Error{
	public static $static = null;
	public $error = array();
	function addError($error_code,$error_str){
		$this->error[] = array('code'=>$error_code,'message'=>$error_str);
	}
	function getError(){
		return $this->error;
	}
	static function getInstance(){
		if(self::$static == null){
			self::$static = new Error();
		}
		return self::$static;
	}
}
class Result{
	public $return;
	public $data = array();
	function addData($variable,$value){
		$this->data[$variable] = $value;
	}
	function addError($code,$message){
		{Error::getInstance() ->addError( $code , $message );} ;
	}
	function merge($rs){
		if(is_array($rs->data)){
			foreach($rs->data as $key=>$value){
				$this->data[$key] = $value;
			}
		}
	}
	function publish($lang){
		if($lang != "bin"){
			$ptn_error_name = "section_error";
			$ptn_error_item_name = "item_error";
			$ptn_message_name = "section_message";
			$ptn_message_item_name = "item_message";
			$ptn_result_name = "section_result";
			if($lang=="xml"){
				$ptn_error_name = "section_error_xml";
				$ptn_error_item_name = "item_error_xml";
				$ptn_message_name = "section_message_xml";
				$ptn_message_item_name = "item_message_xml";
				$ptn_result_name = "section_result_xml";
			}
			$ptn_result = readPattern($_SESSION[ "service_dir" ]  .'/pattern/' .'ptn_'. $ptn_result_name .'.php' );
			$ptn_error = readPattern($_SESSION[ "service_dir" ]  .'/pattern/' .'ptn_'. $ptn_error_name .'.php' );
			$ptn_error_item = readPattern($_SESSION[ "service_dir" ]  .'/pattern/' .'ptn_'. $ptn_error_item_name .'.php' );
			$ptn_message= readPattern($_SESSION[ "service_dir" ]  .'/pattern/' .'ptn_'. $ptn_message_name .'.php' );
			$ptn_message_item = readPattern($_SESSION[ "service_dir" ]  .'/pattern/' .'ptn_'. $ptn_message_item_name .'.php' );
			$ptn_error->items_error = '';
			$ptn_message->items_message = '';
			$ptn_result->result = $this->return?"true":"false";
			$ptn_result->error_lists = '';
			$ptn_result->message_lists = '';
			{ $error  = Error::getInstance() ->error;} ;
			if(is_array($error) && count($error)>0){
				$ptn_error->items_error = '';
				foreach($error as $error_item){
					$ptn_error_item->code = $error_item['code'];
					$ptn_error_item->message = $error_item['message'];
					$ptn_error->items_error .=  $ptn_error_item ->publish() ;
				}
				$ptn_result->error_lists =  $ptn_error ->publish() ;
			}
			if(is_array($this->data) && count($this->data)>0){
				foreach($this->data as $key=>$data){
					$ptn_message_item->message = $data;
					$ptn_message_item->name = $key;
					$ptn_message->items_message .=  $ptn_message_item ->publish() ;
				}
				$ptn_result->message_lists =  $ptn_message ->publish() ;
			}	
			return  $ptn_result ->publish() ;
		}else{
			{ $error  = Error::getInstance() ->error;} ;
			$error_count = count($error);
			$error_size = 0;
			$error_size += 2 ;
			if(is_array($error) && $error_count>0){
				foreach($error as $key=>$error_item){
					$error[$key]['message_bin'] = unpack('C*', $error_item['message'] ) ;
					$error_size += 2 ;
					$error_size += 1 ;
					$error_size += count($error[$key]['message_bin']);
				}
			}
			$error_buf = ByteBuffer::allocate( $error_size ) ;
			if(is_array($error) && $error_count>0){
				DataUtil::writeInt16( $error_buf , $error_count ) ;
				foreach($error as $error_item){
					DataUtil::writeInt16( $error_buf , $error_item['code'] ) ;
					DataUtil::writeBBin( $error_buf , $error_item['message_bin'] ) ;
				}
			}else{
				DataUtil::writeInt16( $error_buf , 0 ) ;
			}
			$error_bin =  $error_buf ->_array() ;
			$error_size = count($error_bin);
			$header = "cbox";
			$header_bin = unpack('C*', $header ) ;
			$header_size = 1  + ((is_array($header_bin))?count($header_bin):0);
			$strdata = $this->data["content"];
			$data = unpack('C*', $strdata ) ;
			$data_size = (is_array($data))?count($data):0;
			$size = 0;
			$size += 1  + $header_size; 
			$size += 1 ; 
			$size += 4 ; 
			$size += $error_size;
			$size += 4 ; 
			$size += $data_size;
			$buf = ByteBuffer::allocate( $size ) ;
			DataUtil::writeBBin( $buf , $header_bin ) ;
			DataUtil::writeInt8( $buf , ($this->return)?1:0 ) ;
			DataUtil::writeInt32( $buf , $this->data['rcode'] ) ;
			DataUtil::writeBytes( $buf ,  $error_bin ) ;
			DataUtil::writeInt32( $buf , $data_size ) ;
			DataUtil::writeBytes( $buf , $data ) ;
			return implode(array_map("chr",   $buf ->_array()  )) ;
		}
	}
}
?>